package org.jeecg.modules.yks.srv.controller;

import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.constant.CommonConstant;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.vo.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.yks.srv.entity.YksEquipmentDevopsMaterialDetail;
import org.jeecg.modules.yks.srv.entity.YksEquipmentDevopsProductDetail;
import org.jeecg.modules.yks.srv.entity.YksEquipmentDevopsStatus;
import org.jeecg.modules.yks.srv.entity.YksEquipmentDevopsService;
import org.jeecg.modules.yks.srv.vo.YksEquipmentDevopsServicePage;
import org.jeecg.modules.yks.srv.service.IYksEquipmentDevopsServiceService;
import org.jeecg.modules.yks.srv.service.IYksEquipmentDevopsMaterialDetailService;
import org.jeecg.modules.yks.srv.service.IYksEquipmentDevopsProductDetailService;
import org.jeecg.modules.yks.srv.service.IYksEquipmentDevopsStatusService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 运维主表
 * @Author: jeecg-boot
 * @Date:   2024-01-16
 * @Version: V1.0
 */
@Api(tags="运维主表")
@RestController
@RequestMapping("/srv/yksEquipmentDevopsService")
@Slf4j
public class YksEquipmentDevopsServiceController {
	@Autowired
	private IYksEquipmentDevopsServiceService yksEquipmentDevopsServiceService;
	@Autowired
	private IYksEquipmentDevopsMaterialDetailService yksEquipmentDevopsMaterialDetailService;
	@Autowired
	private IYksEquipmentDevopsProductDetailService yksEquipmentDevopsProductDetailService;
	@Autowired
	private IYksEquipmentDevopsStatusService yksEquipmentDevopsStatusService;
	
	/**
	 * 分页列表查询
	 *
	 * @param yksEquipmentDevopsService
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "运维主表-分页列表查询")
	@ApiOperation(value="运维主表-分页列表查询", notes="运维主表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<YksEquipmentDevopsService>> queryPageList(YksEquipmentDevopsService yksEquipmentDevopsService,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<YksEquipmentDevopsService> queryWrapper = QueryGenerator.initQueryWrapper(yksEquipmentDevopsService, req.getParameterMap());
		Page<YksEquipmentDevopsService> page = new Page<YksEquipmentDevopsService>(pageNo, pageSize);
		LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
//		Integer USER_IDENTITY_1 = 1; //客户
//		Integer USER_IDENTITY_2 = 2; //监管
//		Integer USER_IDENTITY_3 = 3; //服务
		if (sysUser.getUserIdentity().equals(CommonConstant.USER_IDENTITY_1)) {
			queryWrapper.eq("status","3"); //待客户确认
		}
		IPage<YksEquipmentDevopsService> pageList = yksEquipmentDevopsServiceService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param yksEquipmentDevopsServicePage
	 * @return
	 */
	@AutoLog(value = "运维主表-添加")
	@ApiOperation(value="运维主表-添加", notes="运维主表-添加")
    //@RequiresPermissions("org.jeecg.modules.yks:yks_equipment_devops_service:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody YksEquipmentDevopsServicePage yksEquipmentDevopsServicePage) {
		YksEquipmentDevopsService yksEquipmentDevopsService = new YksEquipmentDevopsService();
		BeanUtils.copyProperties(yksEquipmentDevopsServicePage, yksEquipmentDevopsService);
		yksEquipmentDevopsServiceService.saveMain(yksEquipmentDevopsService, yksEquipmentDevopsServicePage.getYksEquipmentDevopsMaterialDetailList(),yksEquipmentDevopsServicePage.getYksEquipmentDevopsProductDetailList(),yksEquipmentDevopsServicePage.getYksEquipmentDevopsStatusList());
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param yksEquipmentDevopsServicePage
	 * @return
	 */
	@AutoLog(value = "运维主表-编辑")
	@ApiOperation(value="运维主表-编辑", notes="运维主表-编辑")
    //@RequiresPermissions("org.jeecg.modules.yks:yks_equipment_devops_service:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody YksEquipmentDevopsServicePage yksEquipmentDevopsServicePage) {
		YksEquipmentDevopsService yksEquipmentDevopsService = new YksEquipmentDevopsService();
		BeanUtils.copyProperties(yksEquipmentDevopsServicePage, yksEquipmentDevopsService);
		YksEquipmentDevopsService yksEquipmentDevopsServiceEntity = yksEquipmentDevopsServiceService.getById(yksEquipmentDevopsService.getId());
		if(yksEquipmentDevopsServiceEntity==null) {
			return Result.error("未找到对应数据");
		}
		yksEquipmentDevopsServiceService.updateMain(yksEquipmentDevopsService, yksEquipmentDevopsServicePage.getYksEquipmentDevopsMaterialDetailList(),yksEquipmentDevopsServicePage.getYksEquipmentDevopsProductDetailList(),yksEquipmentDevopsServicePage.getYksEquipmentDevopsStatusList());
		return Result.OK("编辑成功!");
	}
	 /**
	  *  状态更改
	  *
	  * @param yksEquipmentDevopsServicePage
	  * @return
	  */
	 @AutoLog(value = "运维主表-编辑")
	 @ApiOperation(value="运维主表-编辑", notes="运维主表-编辑")
	 //@RequiresPermissions("org.jeecg.modules.yks:yks_equipment_devops_service:edit")
	 @PutMapping("/status/confirm")
	 public Result<Object> confirmStatus(@RequestParam(name = "ids", required = true) String ids,
										 @RequestParam(name = "status") String status,
										 @RequestParam(name = "remark", required = false) String remark) {
		 yksEquipmentDevopsServiceService.confirmStatus(Arrays.asList(ids.split(",")), status, remark);
		 return Result.OK("批量提交成功！");
	 }
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "运维主表-通过id删除")
	@ApiOperation(value="运维主表-通过id删除", notes="运维主表-通过id删除")
    //@RequiresPermissions("org.jeecg.modules.yks:yks_equipment_devops_service:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		yksEquipmentDevopsServiceService.delMain(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "运维主表-批量删除")
	@ApiOperation(value="运维主表-批量删除", notes="运维主表-批量删除")
    //@RequiresPermissions("org.jeecg.modules.yks:yks_equipment_devops_service:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.yksEquipmentDevopsServiceService.delBatchMain(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "运维主表-通过id查询")
	@ApiOperation(value="运维主表-通过id查询", notes="运维主表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<YksEquipmentDevopsService> queryById(@RequestParam(name="id",required=true) String id) {
		YksEquipmentDevopsService yksEquipmentDevopsService = yksEquipmentDevopsServiceService.getById(id);
		if(yksEquipmentDevopsService==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(yksEquipmentDevopsService);

	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "运维材料表通过主表ID查询")
	@ApiOperation(value="运维材料表主表ID查询", notes="运维材料表-通主表ID查询")
	@GetMapping(value = "/queryYksEquipmentDevopsMaterialDetailByMainId")
	public Result<List<YksEquipmentDevopsMaterialDetail>> queryYksEquipmentDevopsMaterialDetailListByMainId(@RequestParam(name="id",required=true) String id) {
		List<YksEquipmentDevopsMaterialDetail> yksEquipmentDevopsMaterialDetailList = yksEquipmentDevopsMaterialDetailService.selectByMainId(id);
		return Result.OK(yksEquipmentDevopsMaterialDetailList);
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "产品运维表通过主表ID查询")
	@ApiOperation(value="产品运维表主表ID查询", notes="产品运维表-通主表ID查询")
	@GetMapping(value = "/queryYksEquipmentDevopsProductDetailByMainId")
	public Result<List<YksEquipmentDevopsProductDetail>> queryYksEquipmentDevopsProductDetailListByMainId(@RequestParam(name="id",required=true) String id) {
		List<YksEquipmentDevopsProductDetail> yksEquipmentDevopsProductDetailList = yksEquipmentDevopsProductDetailService.selectByMainId(id);
		return Result.OK(yksEquipmentDevopsProductDetailList);
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "运维状态表通过主表ID查询")
	@ApiOperation(value="运维状态表主表ID查询", notes="运维状态表-通主表ID查询")
	@GetMapping(value = "/queryYksEquipmentDevopsStatusByMainId")
	public Result<List<YksEquipmentDevopsStatus>> queryYksEquipmentDevopsStatusListByMainId(@RequestParam(name="id",required=true) String id) {
		List<YksEquipmentDevopsStatus> yksEquipmentDevopsStatusList = yksEquipmentDevopsStatusService.selectByMainId(id);
		return Result.OK(yksEquipmentDevopsStatusList);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param yksEquipmentDevopsService
    */
    //@RequiresPermissions("org.jeecg.modules.yks:yks_equipment_devops_service:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YksEquipmentDevopsService yksEquipmentDevopsService) {
      // Step.1 组装查询条件查询数据
      QueryWrapper<YksEquipmentDevopsService> queryWrapper = QueryGenerator.initQueryWrapper(yksEquipmentDevopsService, request.getParameterMap());
      LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

      //配置选中数据查询条件
      String selections = request.getParameter("selections");
      if(oConvertUtils.isNotEmpty(selections)) {
         List<String> selectionList = Arrays.asList(selections.split(","));
         queryWrapper.in("id",selectionList);
      }
      //Step.2 获取导出数据
      List<YksEquipmentDevopsService> yksEquipmentDevopsServiceList = yksEquipmentDevopsServiceService.list(queryWrapper);

      // Step.3 组装pageList
      List<YksEquipmentDevopsServicePage> pageList = new ArrayList<YksEquipmentDevopsServicePage>();
      for (YksEquipmentDevopsService main : yksEquipmentDevopsServiceList) {
          YksEquipmentDevopsServicePage vo = new YksEquipmentDevopsServicePage();
          BeanUtils.copyProperties(main, vo);
          List<YksEquipmentDevopsMaterialDetail> yksEquipmentDevopsMaterialDetailList = yksEquipmentDevopsMaterialDetailService.selectByMainId(main.getId());
          vo.setYksEquipmentDevopsMaterialDetailList(yksEquipmentDevopsMaterialDetailList);
          List<YksEquipmentDevopsProductDetail> yksEquipmentDevopsProductDetailList = yksEquipmentDevopsProductDetailService.selectByMainId(main.getId());
          vo.setYksEquipmentDevopsProductDetailList(yksEquipmentDevopsProductDetailList);
          List<YksEquipmentDevopsStatus> yksEquipmentDevopsStatusList = yksEquipmentDevopsStatusService.selectByMainId(main.getId());
          vo.setYksEquipmentDevopsStatusList(yksEquipmentDevopsStatusList);
          pageList.add(vo);
      }

      // Step.4 AutoPoi 导出Excel
      ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
      mv.addObject(NormalExcelConstants.FILE_NAME, "运维主表列表");
      mv.addObject(NormalExcelConstants.CLASS, YksEquipmentDevopsServicePage.class);
      mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("运维主表数据", "导出人:"+sysUser.getRealname(), "运维主表"));
      mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
      return mv;
    }

    /**
    * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("org.jeecg.modules.yks:yks_equipment_devops_service:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
      Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
      for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
          // 获取上传文件对象
          MultipartFile file = entity.getValue();
          ImportParams params = new ImportParams();
          params.setTitleRows(2);
          params.setHeadRows(1);
          params.setNeedSave(true);
          try {
              List<YksEquipmentDevopsServicePage> list = ExcelImportUtil.importExcel(file.getInputStream(), YksEquipmentDevopsServicePage.class, params);
              for (YksEquipmentDevopsServicePage page : list) {
                  YksEquipmentDevopsService po = new YksEquipmentDevopsService();
                  BeanUtils.copyProperties(page, po);
                  yksEquipmentDevopsServiceService.saveMain(po, page.getYksEquipmentDevopsMaterialDetailList(),page.getYksEquipmentDevopsProductDetailList(),page.getYksEquipmentDevopsStatusList());
              }
              return Result.OK("文件导入成功！数据行数:" + list.size());
          } catch (Exception e) {
              log.error(e.getMessage(),e);
              return Result.error("文件导入失败:"+e.getMessage());
          } finally {
              try {
                  file.getInputStream().close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      return Result.OK("文件导入失败！");
    }

}
